﻿//************************************************************************************************
// Copyright © 2014 Steven M. Cohn. All Rights Reserved.
// 
//************************************************************************************************

namespace River.Folderize
{
	using System;
	using System.ComponentModel;
	using System.Windows.Media;


	/// <summary>
	/// Abstract base class for system and custom folders.
	/// </summary>

	internal abstract class Folder : IEditableObject, INotifyPropertyChanged
	{
		private bool isSystemFolder;

		// changeables
		private string name;
		private string path;
		private ImageSource image;

		// editables
		private string originalName;
		private string originalPath;
		private string originalImageRes;
		private ImageSource originalImage;


		/// <summary>
		/// Default constructor for internal framework.
		/// </summary>
		public Folder ()
		{
		}


		/// <summary>
		/// Initialize a new instance for the specified folder GUID.
		/// </summary>
		/// <param name="guid">The GUID of the folder.</param>
		public Folder (string guid)
		{
			this.Guid = guid;
		}


		/// <summary>
		/// Implements INotifyPropetyChanged, required for Image updates.
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;


		/// <summary>
		/// Gets or sets the folder display name.
		/// </summary>
		public string Name
		{
			get
			{
				return name;
			}

			set
			{
				name = value;
				OnPropertyChanged("Name");
			}
		}


		/// <summary>
		/// Gets or sets the unique ID of the folder.
		/// This is a GUID specified using the "B" formatting.
		/// </summary>

		public string Guid { get; set; }


		/// <summary>
		/// Gets or sets the real file system path of the folder.
		/// </summary>
		public string Path
		{
			get
			{
				return path;
			}

			set
			{
				path = value;
				OnPropertyChanged("Path");
			}
		}


		/// <summary>
		/// Gets or set the image associated with the folder.
		/// </summary>
		public ImageSource Image
		{
			get
			{
				return image;
			}

			set
			{
				image = value;
				OnPropertyChanged("Image");
			}
		}


		/// <summary>
		/// Gets or sets the image resource descriptor of the folder.
		/// </summary>
		public string ImageRes
		{
			get;
			set;
		}


		/// <summary>
		/// Gets or sets a value indicating whether this is a known system folder.
		/// </summary>
		public virtual bool IsSystemFolder
		{
			get { return isSystemFolder; }
			set { isSystemFolder = value; }
		}


		/// <summary>
		/// Gets or sets a value indicating whether this folder is "pinned" to
		/// the Windows Explorer navigation panel.
		/// </summary>
		public bool IsPinned
		{
			get;
			set;
		}


		/// <summary>
		/// Implements INotifyPropertyChanged.
		/// </summary>
		/// <param name="name">The name of the property whose value changed.</param>
		protected void OnPropertyChanged (string name)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(name));
			}
		}


		#region IEditableObject Members

		// IEditableObject implemented to allow user to cancel pending editor changes.

		public void BeginEdit ()
		{
			originalName = this.Name;
			originalPath = this.Path;
			originalImage = this.Image;
			originalImageRes = this.ImageRes;
		}

		public void CancelEdit ()
		{
			this.Name = originalName;
			this.Path = originalPath;
			this.Image = originalImage;
			this.ImageRes = originalImageRes;
		}

		public void EndEdit ()
		{
			originalName = null;
			originalPath = null;
			originalImage = null;
			originalImageRes = null;
		}

		#endregion
	}
}
